package com.archermind.boc.dal.business.impl;

import java.util.HashMap;
import java.util.List;

import com.archermind.boc.Logger;
import com.archermind.boc.dal.business.Icommision;
import com.archermind.boc.dal.business.service.BizService;
import com.archermind.boc.dal.business.service.BizTimeoutService;
import com.archermind.boc.dal.business.support.DispatchSupport;
import com.archermind.boc.domain.Biz;
import com.archermind.boc.domain.BizStatus;
import com.archermind.boc.domain.BizTimeout;
import com.archermind.boc.domain.IQ;
import com.archermind.boc.domain.Message;
import com.archermind.boc.domain.Presence;
import com.archermind.boc.ehcache.UserCache;
import com.archermind.boc.ehcache.UserEhcacheImpl;
import com.archermind.boc.ehcache.UserStatus;
import com.archermind.boc.socket.out.IBocSocketWriter;
import com.archermind.sessionpool.BizSession;
import com.archermind.sessionpool.BizSessionPool;

public class CommisionImpl implements Icommision {
	private BizService bziService;
	private BizTimeoutService biztimeoutservice;
	private UserEhcacheImpl userEhcache;
	private IBocSocketWriter bocSocketWriter;
	private Logger log = Logger.getLogger(CommisionImpl.class.getName());

	public boolean addSessionBiz(int Bizid, long createTime, String BizTypeid) {
		// TODO Auto-generated method stub
		boolean falg = true;
		BizSession session = new BizSession();
		BizTimeout timeout = biztimeoutservice.findByTypeId(BizTypeid);
		if (timeout != null) {
			session.setBizTimeOutSecond(timeout.getTimeout());
		} else {
			session.setBizTimeOutSecond(BizStatus.DEFLAUT_TIMEOUT);
			log.error("default timeout 100s !");
		}
		session.setBizid(Bizid);
		session.setCreateTime(createTime);
		BizSessionPool.addBiz(session);

		// if (timeout != null) {
		// session.setBizTimeOutSecond(timeout.getTimeout());
		// session.setBizid(Bizid);
		// session.setCreateTime(createTime);
		// BizSessionPool.addBiz(session);
		// } else {
		// falg = false;
		// log.error("timeout is null !");
		// }
		return falg;
	}

	public void deportBiz(Biz biz) {
		// TODO Auto-generated method stub
		if (!bziService.bizAdded(biz)) {
			log.error("biz insert is fail  !");
		}
	}

	public UserCache getUserCache(IQ iq, Biz biz) {
		UserCache userCache = userEhcache.getUserCachebySessionId(iq
				.getSessionID());
		if (userCache != null) {
			userCache = userEhcache
					.assignToUser(userCache.getUserid(), biz, iq);
		}
		return userCache;
	}

	public void write(Message message) {
		// TODO Auto-generated method stub
		bocSocketWriter.write(message);
	}

	public void write(IQ iq) {
		// TODO Auto-generated method stub
		log.info("------------------send iq----------------------------");
		bocSocketWriter.write(iq);
	}

	public boolean modifyBiz(Biz biz, UserCache userCache) {
		biz.setToBankNum(userCache.getUserid());
		return bziService.modifyBiz(biz);
	}

	public void modifyUserCache(UserCache userCache) {
		// TODO Auto-generated method stub
		userEhcache.modifyUserCache(userCache);
	}

	public boolean saveTurnTextMessage(Message message) {
		boolean state = true;
		Biz biz = DispatchSupport.getBizByUserCache(message, userEhcache);
		if (biz != null) {
			DispatchSupport.getXdataByText(biz, message);
			if (bziService.bizAdded(biz)) {
				String id = Transmit.transmitUser(message.getSessionID());
				message.setSessionID(id);
				write(message);
			} else {
				state = false;
			}
		} else {
			state = false;
			log.error("MessageBiz is null !");
		}
		return state;
	}

	public boolean saveTurnImage(Message message,
			HashMap<String, List<String>> map) {
		boolean state = true;
		Biz biz = DispatchSupport.getBizByUserCache(message, userEhcache);
		if (biz != null) {
			DispatchSupport.getXdataByImage(biz, message, map);
			if (bziService.bizAdded(biz)) {
				String id = Transmit.transmitUser(message.getSessionID());
				message.setSessionID(id);
				write(message);
			} else {
				state = false;
			}
		} else {
			state = false;
			log.error("MessageBiz is null !");
		}
		return state;
	}

	public void acceptRespone(Message message) {
		String sessionID = message.getSessionID();
		String newSessionID = Transmit.transmitUser(sessionID);
		message.setSessionID(newSessionID);
		write(message);
		// DispatchSupport.closeBiz(message, map, bocSocketWriter, userEhcache,
		// "同意");
	}

	public void rejectRespone(Message message) {
		String sessionID = message.getSessionID();
		String newSessionID = Transmit.transmitUser(sessionID);
		message.setSessionID(newSessionID);
		write(message);
		// DispatchSupport.closeBiz(message, map, bocSocketWriter, userEhcache,
		// "拒绝");
	}

	// public void cancelSendMessage(Message message, HashMap<String,
	// List<String>> map) {
	// DispatchSupport.closeBiz(message, map, bocSocketWriter, userEhcache,
	// "取消");
	// }

	public int getTimeOut(String typeId) {
		BizTimeout bizTime = biztimeoutservice.findByTypeId(typeId);
		if (bizTime != null) {
			return bizTime.getTimeout();
		} else {
			return BizStatus.DEFLAUT_TIMEOUT;
		}
	}

	public void modifyState(Presence presence) {
		UserCache userCache = userEhcache.getUserCachebySessionId(presence
				.getSessionID());
		if (userCache != null) {
			if (presence.getShow() != null && "1".equals(presence.getShow())) {
				userCache.setStatus(UserStatus.USER_SIGNIN);
			}
			if (presence.getShow() != null && "2".equals(presence.getShow())) {
				userCache.setStatus(UserStatus.USER_SIGNOUT);
			}
			if (presence.getShow() != null && "3".equals(presence.getShow())) {
				userCache.setStatus(UserStatus.USER_TEMPOUT);
			}
			modifyUserCache(userCache);
		} else {
			log.info("message: presence: update userCache fail !"
					+ presence.getSessionID());
		}

	}

	public BizService getBziService() {
		return bziService;
	}

	public void setBziService(BizService bziService) {
		this.bziService = bziService;
	}

	public BizTimeoutService getBiztimeoutservice() {
		return biztimeoutservice;
	}

	public void setBiztimeoutservice(BizTimeoutService biztimeoutservice) {
		this.biztimeoutservice = biztimeoutservice;
	}

	public UserEhcacheImpl getUserEhcache() {
		return userEhcache;
	}

	public void setUserEhcache(UserEhcacheImpl userEhcache) {
		this.userEhcache = userEhcache;
	}

	public IBocSocketWriter getBocSocketWriter() {
		return bocSocketWriter;
	}

	public void setBocSocketWriter(IBocSocketWriter bocSocketWriter) {
		this.bocSocketWriter = bocSocketWriter;
	}

}
